<?php
/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author Eddie Drapkin <eddie@frameweld.com> 
 * @license http://www.gnu.org/licenses/lgpl-3.0.txt
 * @package Solphr
 * @subpackage Query
 * @version 0.1-pre
 */

/**
 * Class is used to represent a date range for Solr.
 * 
 * @author Eddie Drapkin
 * @copyright Copyright 2009, Frameweld, LLC. (http://www.frameweld.com)
 * @license http://www.gnu.org/licenses/lgpl-3.0.txt
 * @package Solphr
 * @subpackage Query
 */

class Solphr_Query_DateRange implements Solphr_Query_Interface {
	
	/**
	 * Class constructor
	 * @param mixed $start The start date, leave null for blank.
	 * @param mixed $end The end date, leave null for blank.
	 */
	public function __construct(DateTime $start = null, DateTime $end = null, $field = null) {
		$utc = new DateTimeZone('UTC');
		
		if(!isset($start)) {
			//Null means wildcard
			$this->start = '*';
		} else {
			//DateTime instance means an exact time
			$this->start = $start->setTimezone($utc)->format('Y-m-d\TH:i:s\Z');
		} 
		
		
		if(!isset($end)) {
			$this->end = '*';
		} else {
			$this->end = $end->setTimezone($utc)->format('Y-m-d\TH:i:s\Z');
		}
		
		$this->field = isset($field) ? $field : false;
	}
	
	/**
	 * @see Solphr/Query/Solphr_Query#__toString()
	 */
	public function __toString() {
		if(empty($this->field)) {
			return "[{$this->start} TO {$this->end}]";
		} else {
			return "{$this->field}:[{$this->start} TO {$this->end}]";
		}
	}
}